﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;

namespace CommunitySystem.PageView
{
    public partial class BianZhiWindow_New : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.BianZhiModel model = new EmpbasicService.BianZhiModel();
        public int ID;
        public int OrgID;
        public bool IsNew = false;
        public BianZhiWindow_New(int orgID, int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            OrgID = orgID;
            IsNew = isnew;
            
            if (IsNew)
            {
                this.Title = "新增编制信息";
                Client.GetBianZhiModelForTongChouCompleted += 
                    new EventHandler<EmpbasicService.GetBianZhiModelForTongChouCompletedEventArgs>(Client_GetBianZhiModelForTongChouCompleted);
                Client.GetBianZhiModelForTongChouAsync(OrgID);
                
            }
            else
            {
                this.Title = "编辑编制信息";
                Client.GetBianZhiModelCompleted += new EventHandler<EmpbasicService.GetBianZhiModelCompletedEventArgs>(Client_GetBianZhiModelCompleted);
                Client.GetBianZhiModelAsync("", ID);
            }
        }

        void Client_GetBianZhiModelForTongChouCompleted(object sender, EmpbasicService.GetBianZhiModelForTongChouCompletedEventArgs e)
        {
            model = new EmpbasicService.BianZhiModel();
            if(e.Result != null && e.Result.Count > 0)
            {
                foreach(BianZhiModel b in e.Result)
                {
                    if (!string.IsNullOrEmpty(b.JiGuanXingZheng))
                        model.JiGuanXingZheng = b.JiGuanXingZheng;
                    if (!string.IsNullOrEmpty(b.ZhengFaXingZheng))
                        model.ZhengFaXingZheng = b.ZhengFaXingZheng;
                    if (!string.IsNullOrEmpty(b.FuKeLingDao))
                        model.FuKeLingDao = b.FuKeLingDao;
                    if (!string.IsNullOrEmpty(b.FuKeFeiLingDao))
                        model.FuKeFeiLingDao = b.FuKeFeiLingDao;
                    if (!string.IsNullOrEmpty(b.CanZhaoGuanLi))
                        model.CanZhaoGuanLi = b.CanZhaoGuanLi;
                    if (!string.IsNullOrEmpty(model.JiGuanXingZheng))
                        break;
                }
                
            }
            xiangkefu.Visibility = System.Windows.Visibility.Visible;
            xiangkezheng.Visibility = System.Windows.Visibility.Visible;
            this.DataContext = model;
        }

        void Client_GetBianZhiModelCompleted(object sender, EmpbasicService.GetBianZhiModelCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            if (model.ZhengKeFeiLingDaoTC == true)
            {
                xiangkefu1.Visibility = System.Windows.Visibility.Visible;
                xiangkefu.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                xiangkefu.Visibility = System.Windows.Visibility.Visible;
                xiangkefu1.Visibility = System.Windows.Visibility.Collapsed;
                
            }
            if (model.ZhengKeLingDaoTC == true)
            {
                xiangkezheng1.Visibility = System.Windows.Visibility.Visible;
                xiangkezheng.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                xiangkezheng.Visibility = System.Windows.Visibility.Visible;
                xiangkezheng1.Visibility = System.Windows.Visibility.Collapsed;

            }
            this.DataContext = model;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(model.JiGuanXingZheng))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("机关行政编制数不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(model.ZhengFaXingZheng))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("政法行政编制数不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(model.FuKeLingDao))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("主任科员不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(model.FuKeFeiLingDao))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("副主任科员不能为空！");
                return;
            }
            if (IsNew == true)
            {
                model.OrgID = OrgID;
                Client.AddBianZhiModelCompleted += new EventHandler<EmpbasicService.AddBianZhiModelCompletedEventArgs>(Client_AddBianZhiModelCompleted);
                Client.AddBianZhiModelAsync(model);
            }
            else
            {
                Client.UpdateBianZhiModelCompleted += new EventHandler<EmpbasicService.UpdateBianZhiModelCompletedEventArgs>(Client_UpdateBianZhiModelCompleted);
                Client.UpdateBianZhiModelAsync(model);
            }
        }

        void Client_UpdateBianZhiModelCompleted(object sender, EmpbasicService.UpdateBianZhiModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                //result();
                this.DialogResult = true;
            }
        }

        void Client_AddBianZhiModelCompleted(object sender, EmpbasicService.AddBianZhiModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                //result();
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }


         private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
         private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
         {
             Match m = Regex.Match(((TextBox) sender).Text, pattern); // 匹配正则表达式
             if (!m.Success) // 输入的不是数字
             {
                 SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                 ((TextBox) sender).Text = "";
                 return;
             }
         }
    }
}
